package com.youths.controller;

import com.google.gson.Gson;
import com.youths.domain.LabelConsumer;
import com.youths.domain.YouthInfo;
import com.youths.response.Result;
import com.youths.service.YouthInfoService;
import com.youths.util.IdCardVerification;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: KingRainGrey
 * Date: 2020/6/24
 */
@Controller
@RequestMapping(value = "/")
public class YouthInfoController {
    @Autowired
    YouthInfoService youthInfoService;

    @GetMapping(value = "add")
    public String add(HttpSession session) {
        return "youthInfo/add";
    }

    @GetMapping(value = "list")
    public String list(HttpSession session, Model model) {
        try {
            LabelConsumer consumer = (LabelConsumer) session.getAttribute("user");
            if (consumer != null) {
                int id = consumer.getConsumerId();
                List<YouthInfo> youthInfoList = youthInfoService.getInfoList(id);
                model.addAttribute("youths", youthInfoList);
            }else {
                model.addAttribute("msg", "登录已经过期");
            }
        }catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", e.getStackTrace());
        }
        return "youthInfo/list";
    }



    @PostMapping(value = "add")
    public String add(@RequestParam("youthName") String youthName,
                      @RequestParam("sexId") String sexId,
                      @RequestParam("nationId") String nationId,
                      @RequestParam("birth") String birth,
                      @RequestParam("idCard") String idCard,
                      @RequestParam("address") String address,
                      @RequestParam("place") String place,
                      @RequestParam("fHeight") String fHeight,
                      @RequestParam("mHeight") String mHeight,
                      HttpSession session,
                      Model model) {

        String nationList[] = {"汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族",
                "朝鲜族", "满族", "侗族", "瑶族", "白族", "土家族", "哈尼族", "哈萨克族", "傣族", "黎族",
                "僳僳族", "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族",
                "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族", "毛南族", "仡佬族", "锡伯族", "阿昌族",
                "普米族", "塔吉克族", "怒族", "乌孜别克族", "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族",
                "京族", "塔塔尔族", "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族"};

        try {
            LabelConsumer consumer = (LabelConsumer) session.getAttribute("user");
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            if (consumer != null) {
                //String idResult = IdCardVerification.IdentityCardVerification(idCard);
                String idResult = "";
                if (idResult.length() == 0) {
                    int addResult = youthInfoService.addYouthInfo(idCard, youthName, Integer.parseInt(sexId), address, place,
                            nationList[Integer.parseInt(nationId)], sdf.parse(birth), Float.parseFloat(fHeight),
                            Float.parseFloat(mHeight), consumer.getConsumerId(), 0);
                    if (addResult > 0) {
                        model.addAttribute("msg", "添加成功");
                    }else {
                        model.addAttribute("msg", "添加失败");
                    }
                }else {
                    model.addAttribute("msg", idResult);
                }
            }else {
                model.addAttribute("msg" , "登录已过期");
                return "/login";
            }
        }catch (Exception e) {
            model.addAttribute("msg", e.getStackTrace());
            e.printStackTrace();
        }

        return "youthInfo/add";
    }


    @PostMapping(value = "update", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String update(@RequestParam("youthId") Integer youthId,
                         @RequestParam("name") String name,
                         @RequestParam("address") String address,
                         @RequestParam("mHeight") Float mHeight,
                         @RequestParam("fHeight") Float fHeight,
                         HttpSession session) {
        Gson gson = new Gson();
        Map<String, Object> resMap = new HashMap<>();
        try {
            LabelConsumer consumer = (LabelConsumer) session.getAttribute("user");
            if (consumer != null) {
                //权限检查
                if (youthInfoService.check(consumer.getConsumerId(), youthId)){
                    int result = youthInfoService.update(youthId, name, address, fHeight, mHeight, 1);
                    if (result > 0) {
                        resMap.put("res", 1);
                    }
                    return gson.toJson(resMap);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //error
        resMap.put("res", 0);
        return gson.toJson(resMap);
    }

    @GetMapping("/back/getYouthInfoList")
    @ResponseBody
    public Result getYouthInfoList(@RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("youthId") Integer youthId,
                                   @RequestParam("name") String name,
                                   @RequestParam("card") String card,
                                   @RequestParam("account") String account){
        return Result.success(youthInfoService.getYouthInfoList(pageNum,youthId,name,card,account));
    }

    @PostMapping("/back/updateYouthInfo")
    @ResponseBody
    public Result updateYouthInfo(@RequestParam("youthId") Integer youthId,
                                  @RequestParam("cardId") String cardId,
                                  @RequestParam("name") String name,
                                  @RequestParam("sex") Integer sex,
                                  @RequestParam("address") String address,
                                  @RequestParam("place") String place,
                                  @RequestParam("nation") String nation,
                                  @RequestParam("fatherHeight") Integer fatherHeight,
                                  @RequestParam("motherHeight") Integer motherHeight,
                                  @RequestParam("state") Integer state){
        return Result.success(youthInfoService.updateYouthInfo(youthId,cardId,name,sex,address,place,nation,fatherHeight,motherHeight,state));
    }
}
